package com.systop.payment.service.impl;

import com.systop.payment.entity.ClassifyEntity;
import com.systop.payment.entity.ProductEntity;
import com.systop.payment.service.ClassifyService;
import com.systop.payment.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systop.common.utils.PageUtils;
import com.systop.common.utils.Query;

import com.systop.payment.dao.OrderSubjoinDao;
import com.systop.payment.entity.OrderSubjoinEntity;
import com.systop.payment.service.OrderSubjoinService;
import org.springframework.util.StringUtils;


@Service("orderSubjoinService")
public class OrderSubjoinServiceImpl extends ServiceImpl<OrderSubjoinDao, OrderSubjoinEntity> implements OrderSubjoinService {

    @Autowired
    private ProductService productService;

    @Autowired
    private ClassifyService classifyService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderSubjoinEntity> page = this.page(
                new Query<OrderSubjoinEntity>().getPage(params),
                new QueryWrapper<OrderSubjoinEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean saveByParentId(Integer id, List<OrderSubjoinEntity> subjoinList) {
        //如果id为空或者子表数据为空则回滚数据
        if(id == null || id.equals("")||subjoinList.size()!=1){
            return false;
        }

        //获取唯一的一条数据
        OrderSubjoinEntity subjoinEntity = subjoinList.get(0);
        //获取商品是否存在
        ProductEntity sid = productService.getOne(new QueryWrapper<ProductEntity>().eq("id", subjoinEntity.getSid()));
        //检查数据是否存在
        if(subjoinEntity==null||sid==null){
            System.out.println("唯一数据或商品不存在");
            return false;
        }

        //验证数据是否合法
        boolean b = this.ValidatedData(subjoinEntity);
        if(!b){
            return false;
        }

        //获取指定商品信息
        ProductEntity productEntity = productService.getOne(
                new QueryWrapper<ProductEntity>().eq("id", subjoinEntity.getSid()));
        //通过获取的商品id填写其他信息
        ClassifyEntity classifyEntity = classifyService.getById(productEntity.getCid());

        //指定父表对应id
        subjoinEntity.setParentId(id);
        //设置分类
        subjoinEntity.setCid(productEntity.getCid());
        //设置订单商品名称为 (分类-商品名)
        subjoinEntity.setPName(classifyEntity.getClassifyName()+"-"+productEntity.getProductName());
        //设置单位
        subjoinEntity.setPUnitName(productEntity.getUnit());
        //设置单价
        subjoinEntity.setPUnitPrice(productEntity.getUnitPrice());
        //计算总价
        int total = Integer.parseInt(String.valueOf(subjoinEntity.getPUnitPrice()))
                 * Integer.parseInt(String.valueOf(subjoinEntity.getPNum()));
        subjoinEntity.setPTotalPrice(BigDecimal.valueOf(total));
        //添加子表数据
        this.save(subjoinEntity);
        return true;
    }

    //验证数据是否合法(如产品表中库存是否充足)
    public boolean ValidatedData(OrderSubjoinEntity subjoinEntity){
        ProductEntity productEntity = productService.getOne(
                new QueryWrapper<ProductEntity>().eq("id", subjoinEntity.getSid()));

        //当数据不合法
        if(productEntity==null){
            return false;
        }

        //获取库存量和订单数量
        float nums = productEntity.getNums();
        float num = Float.valueOf(String.valueOf(subjoinEntity.getPNum()));
        //验证库存是否足够
        if((nums - num)<0){
            return false;
        }else{
            //库存充足的情况下修改库存
            ProductEntity updateEntity = new ProductEntity();
            updateEntity.setId(productEntity.getId());
            updateEntity.setNums((float)(nums - num));
            productService.updateById(productEntity);
        }
        return true;
    }
}